<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户类型" prop="userType" v-show="change">
        <el-select v-model="queryParams.userType" placeholder="请选择用户类型" clearable>
          <el-option
            v-for="dict in dict.type.biz_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sexCode" v-show="change">
        <el-select v-model="queryParams.sexCode" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row v-show="change" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:info:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:info:export']"
          v-show="false"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @current-change="handleCurrentChange" highlight-current-row @selection-change="handleSelectionChange">
      <!-- <el-table-column v-show="!change" type="selection" width="55" align="center" /> -->

      <el-table-column width="75"  v-if="!change">
        <template slot-scope="scope">
          <el-radio :label="scope.row.id" v-model="selectedRow">&nbsp;</el-radio>
        </template>
      </el-table-column>

      <el-table-column v-if="change" label="编号" align="center" prop="id" />
      <el-table-column label="用户类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="userPhone" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="性别" align="center" prop="sexCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sexCode"/>
        </template>
      </el-table-column>
      <el-table-column label="发证机关" align="center" prop="issuingAuthority" />
      <el-table-column label="文化程度" align="center" prop="educationCode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.education" :value="scope.row.educationCode"/>
        </template>
      </el-table-column> 
      <el-table-column label="民族" align="center" prop="nationCode" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.nation" :value="scope.row.nationCode"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="change" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0 && change"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true" >
        <!-- <div style="display: flex; flex-direction: column; margin-bottom: 20px;">
          <el-form-item label="证件照" prop="photo">
            <ImageUpload :limit="1" @getaDOCtament="getaDOCtament" :value="form.photo" :displays="true" :isShowTip="false"/>
          </el-form-item>
        </div> -->

        <el-form-item label="用户类型" prop="userType">
              <el-select v-model="form.userType" placeholder="请选择用户类型" style="width: 200px;">
                <el-option
                  v-for="dict in dict.type.biz_user_type"
                  :key="`${dict.value}-${dict.label}`"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

        <el-form-item label="公司名称" prop="company" v-if="showCompanyInput">
          <el-input v-model="form.company" placeholder="请输入公司名称" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>

        <el-form-item label="性别" prop="sexCode">
          <el-select v-model="form.sexCode" placeholder="请选择性别" style="width: 200px;">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="`${dict.value}-${dict.label}`"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="手机号" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入手机号" />
        </el-form-item>

        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        
        <!-- <el-form-item label="生日" prop="birthday" >
          <el-date-picker clearable style="width: 200px;"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item> -->
       
        <el-form-item label="住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入住址" />
        </el-form-item>
        
        <el-form-item label="文化程度" prop="educationCode">
          <!-- <el-input v-model="form.educationCode" placeholder="请输入文化程度" /> -->
          <el-select v-model="form.educationCode" placeholder="请选择文化程度" style="width: 200px;">
            <el-option
              v-for="dict in dict.type.education"
              :key="`${dict.value}-${dict.label}`"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nationCode">
          <!-- <el-input v-model="form.nationCode" placeholder="请输入民族" /> -->
          <el-select v-model="form.nationCode" placeholder="请选择民族" style="width: 200px;">
            <el-option
              v-for="dict in dict.type.nation"
              :key="`${dict.value}-${dict.label}`"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item label="发证机关" prop="issuingAuthority">
          <el-input v-model="form.issuingAuthority" placeholder="请输入发证机关" />
        </el-form-item>

        <el-form-item label="身份证有效期" prop="idCardExpirationDate">
          <el-input v-model="form.idCardExpirationDate" placeholder="请输入身份证有效期" />
        </el-form-item>

        <!-- <el-form-item label="微信openid" prop="wxOpenid" :hidden="true">
          <el-input v-model="form.wxOpenid" placeholder="请输入微信openid" />
        </el-form-item>
        <el-form-item label="微信token" prop="wxToken" :hidden="true">
          <el-input v-model="form.wxToken" placeholder="请输入微信token" />
        </el-form-item> -->
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-col>
          <el-form-item label="实名状态" prop="realCheck">
            <el-radio-group v-model="form.realCheck">
              <el-radio
                disabled 
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
        <!-- <el-form-item label="删除标志" prop="delFlag" :hidden="true">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addInfo, delInfo, getInfo, listInfo, updateInfo } from "@/api/user/info";

export default {
  name: "Info",
  dicts: ['biz_user_type', 'sys_yes_no', 'sys_user_sex', 'sys_normal_disable','education','nation'],
  props: {
    change: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务用户表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: null,                         
        userName: null,
        userPhone: null,
        idCard: null,
        sexCode: null,
        realCheck: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userType: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        userName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        userPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        sexCode: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
      },
      selectedRow: '',
    };
  },
  created() {
    console.log('this.change',this.change);
    
    if(!this.change){
      this.queryParams.realCheck = '2'
    }
    this.getList();
    console.log('change',this.change);
    
  },
  watch: {
    change: (value)=>{
      console.log('value',value);
      
    }
  },
  computed: {
   
    showCompanyInput() {
      // 根据用户类型判断是否显示公司名称输入组件
      return this.form.userType === '2'; // 假设企业用户的值为 '2'
    }
  },
  methods: {
    handleCurrentChange(val) {
      console.log('val',val);
      this.selectedRow = val ? val.id : null;
      this.$emit('rowRadioHandle', val);
    },
    /** 查询服务用户列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userType: null,
        userName: null,
        userPhone: null,
        idCard: null,
        sexCode: null,
        birthday: null,
        photo: null,
        address: null,
        idCardExpirationDate: null,
        issuingAuthority: null,
        educationCode: null,
        nationCode: null,
        company: null,
        wxOpenid: null,
        wxToken: null,
        status: null,
        realCheck: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务用户";
      });
    },
        //获取上传文件url
    getaDOCtament(urls) {
     this.form.photo=urls
      console.log("上传文件url", urls);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务用户编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
